export type EleOfArr<T> = T extends Array<infer E> ? E : never

export type ItemType<T extends object[]> = {
  [K in keyof EleOfArr<T>]: EleOfArr<T>[K]
}

export function getSubItemFromArr<T extends ItemType<T>[], K extends keyof EleOfArr<T>>(t: T, ...keys: K[]): Pick<EleOfArr<T>, K>[] {
  return t.map(item => {
    return keys.reduce((pre, _cur, index) => {
      return {
        ...pre,
        [keys[index]]: item[keys[index]]
      }
    }, {})
  }) as Pick<EleOfArr<T>, K>[]
}

function getOneItemValuesFromArr<T extends ItemType<T>[], K extends keyof EleOfArr<T>, E = EleOfArr<T>>(arr: T, k: K) {
  return arr.map(({ [k]: v }: E) => {
    return v
  })
}

function getNoReptValuesItem(arr: any[]) {
  const ret: any[] = []
  arr.filter(item => !ret.includes(item) && ret.push(item))
  return ret
}

function getNoReptItem<T extends ItemType<T>[], K extends keyof EleOfArr<T> = keyof EleOfArr<T>>(arr: T, k: K) {
  const ret: ItemType<T>[] = []
  // 1 获取对象中某个元素的值组成的数组
  const oneItemValues: any[] = getOneItemValuesFromArr(arr, k)

  // 2 对oneItemValues数组去重
  let noRepOneItemValues = getNoReptValuesItem(oneItemValues)
  // 3 对对象去重
  arr.map(item => {
    if (noRepOneItemValues.includes(item[k])) {
      noRepOneItemValues.splice(noRepOneItemValues.indexOf(item[k]), 1)
      ret.push(item)
    }
  })
  return ret
}

const a = [
  {
    secondctgyid: 1,
    secctgyname: '0-2岁',
    firstctgyId: 1,
    thirdctgyid: 1,
    thirdctgyname: '图画故事',
    secctgyid: 1
  },
  {
    secondctgyid: 1,
    secctgyname: '0-2岁',
    firstctgyId: 1,
    thirdctgyid: 2,
    thirdctgyname: '认知',
    secctgyid: 1
  },
  {
    secondctgyid: 1,
    secctgyname: '0-2岁',
    firstctgyId: 1,
    thirdctgyid: 3,
    thirdctgyname: '益智游戏',
    secctgyid: 1
  },
  {
    secondctgyid: 1,
    secctgyname: '0-2岁',
    firstctgyId: 1,
    thirdctgyid: 4,
    thirdctgyname: '纸板书',
    secctgyid: 1
  },
  {
    secondctgyid: 1,
    secctgyname: '0-2岁',
    firstctgyId: 1,
    thirdctgyid: 5,
    thirdctgyname: '艺术课堂',
    secctgyid: 1
  },
  {
    secondctgyid: 1,
    secctgyname: '0-2岁',
    firstctgyId: 1,
    thirdctgyid: 6,
    thirdctgyname: '入园准备',
    secctgyid: 1
  },
  {
    secondctgyid: 2,
    secctgyname: '3-6岁',
    firstctgyId: 1,
    thirdctgyid: 7,
    thirdctgyname: '绘本',
    secctgyid: 2
  },
  {
    secondctgyid: 2,
    secctgyname: '3-6岁',
    firstctgyId: 1,
    thirdctgyid: 8,
    thirdctgyname: '科普百科',
    secctgyid: 2
  },
  {
    secondctgyid: 2,
    secctgyname: '3-6岁',
    firstctgyId: 1,
    thirdctgyid: 9,
    thirdctgyname: '少儿英语',
    secctgyid: 2
  },
  {
    secondctgyid: 2,
    secctgyname: '3-6岁',
    firstctgyId: 1,
    thirdctgyid: 10,
    thirdctgyname: '乐高学习',
    secctgyid: 2
  },
  {
    secondctgyid: 2,
    secctgyname: '3-6岁',
    firstctgyId: 1,
    thirdctgyid: 11,
    thirdctgyname: '入学准备',
    secctgyid: 2
  },
  {
    secondctgyid: 3,
    secctgyname: '7-10岁',
    firstctgyId: 1,
    thirdctgyid: 12,
    thirdctgyname: '文学',
    secctgyid: 3
  },
  {
    secondctgyid: 3,
    secctgyname: '7-10岁',
    firstctgyId: 1,
    thirdctgyid: 13,
    thirdctgyname: '科普百科',
    secctgyid: 3
  },
  {
    secondctgyid: 3,
    secctgyname: '7-10岁',
    firstctgyId: 1,
    thirdctgyid: 14,
    thirdctgyname: '卡通动漫',
    secctgyid: 3
  },
  {
    secondctgyid: 3,
    secctgyname: '7-10岁',
    firstctgyId: 1,
    thirdctgyid: 15,
    thirdctgyname: '童话',
    secctgyid: 3
  },
  {
    secondctgyid: 3,
    secctgyname: '7-10岁',
    firstctgyId: 1,
    thirdctgyid: 16,
    thirdctgyname: '少儿英语',
    secctgyid: 3
  },
  {
    secondctgyid: 4,
    secctgyname: '11-14岁',
    firstctgyId: 1,
    thirdctgyid: 17,
    thirdctgyname: '励志',
    secctgyid: 4
  },
  {
    secondctgyid: 4,
    secctgyname: '11-14岁',
    firstctgyId: 1,
    thirdctgyid: 18,
    thirdctgyname: '地理',
    secctgyid: 4
  },
  {
    secondctgyid: 4,
    secctgyname: '11-14岁',
    firstctgyId: 1,
    thirdctgyid: 19,
    thirdctgyname: '政治',
    secctgyid: 4
  },
  {
    secondctgyid: 4,
    secctgyname: '11-14岁',
    firstctgyId: 1,
    thirdctgyid: 20,
    thirdctgyname: '趣味幽默',
    secctgyid: 4
  },
  {
    secondctgyid: 4,
    secctgyname: '11-14岁',
    firstctgyId: 1,
    thirdctgyid: 21,
    thirdctgyname: '少儿英语',
    secctgyid: 4
  },
  {
    secondctgyid: 4,
    secctgyname: '11-14岁',
    firstctgyId: 1,
    thirdctgyid: 22,
    thirdctgyname: '益智游戏',
    secctgyid: 4
  },
  {
    secondctgyid: 4,
    secctgyname: '11-14岁',
    firstctgyId: 1,
    thirdctgyid: 23,
    thirdctgyname: '艺术课堂',
    secctgyid: 4
  },
  {
    secondctgyid: 4,
    secctgyname: '11-14岁',
    firstctgyId: 1,
    thirdctgyid: 24,
    thirdctgyname: '游戏/手工',
    secctgyid: 4
  },
  {
    secondctgyid: 4,
    secctgyname: '11-14岁',
    firstctgyId: 1,
    thirdctgyid: 25,
    thirdctgyname: '绘画',
    secctgyid: 4
  }
]

// const t = getSubItemFromArr(a, 'secondctgyid', 'secctgyname')

// const tt = getOneItemValuesFromArr(t, 'secondctgyid')

// const ttt = getNoReptValuesItem(tt)
const tttt = getNoReptItem(a, 'secondctgyid')
console.log(tttt)
